<?php

namespace app\api\controller;

use app\common\logic\BankLogic;
use app\common\controller\Api;

/**
 * @ApiSector (用户端)
 * 银行卡接口
 */
class Bank extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];
    /**
     * @Author LK
     * @date: 2023-11-28 
     * @ApiTitle (银行卡管理)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function getBankList()
    {
        $list = BankLogic::getBankList($this->auth->id);
        $this->success('', $list);
    }
    /**
     * @Author LK
     * @date: 2023-11-28 
     * @ApiTitle (获取默认银行卡)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function getDefaultBank()
    {

        $list = BankLogic::getDefaultBank($this->auth->id);
        $this->success('', $list);
    }
    /**
     * @Author LK
     * @date: 2023-11-28 
     * @ApiTitle (设置默认银行卡)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function setDefaultBank()
    {
        $params = request()->param();
        extract($params);
        try {
            BankLogic::setDefaultBank($id, $this->auth->id);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('成功');
    }
    /**
     * @Author LK
     * @date: 2023-11-28 
     * @ApiTitle (银行卡详情)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function getBankInfo()
    {
        $params = request()->param();
        $msg = $this->validate(
            $params,
            [
                'id' => 'require',
            ]
        );
        if (true !== $msg) {
            $this->error($msg);
        }
        extract($params);
        $list = BankLogic::getBankInfo($id);
        $this->success('', $list);
    }
    /**
     * @Author LK
     * @date: 2023-11-28 
     * @ApiTitle (添加银行卡)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID(添加传0)")
    //  * @ApiParams (name="cardholder", type="string", required=true, sample="", description="开户人")
     * @ApiParams (name="bank_name", type="string", required=true, sample="", description="开户行")
     * @ApiParams (name="outlets", type="string", required=true, sample="", description="开户支行")
     * @ApiParams (name="account", type="string", required=true, sample="", description="开户账号")
     * @ApiParams (name="is_default", type="string", required=true, sample="", description="0=不默认,1=默认")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function addEditBank()
    {
        $params = request()->param();
        $rule = [
            'id' => 'require',
            // 'cardholder' => 'require',
            'bank_name' => 'require',
            'outlets' => 'require',
            'account' => 'require',
            'is_default' => 'require',
        ];
        $msg = [
            // 'cardholder.require' => '开户人不能为空',
            'bank_name.require' => '开户行不能为空',
            'outlets.require' => '开户支行不能为空',
            'account.require' => '开户账号不能为空',
        ];
        $validate = $this->validate($params, $rule, $msg);
        if ($validate !== true) {
            $this->error($validate);
        }
        try {
            $params['user_id'] = $this->auth->id;
            BankLogic::addEditBank($params);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('成功');
    }
    /**
     * @Author LK
     * @date: 2023-11-28 
     * @ApiTitle (删除银行卡)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function delBank()
    {
        $params = request()->param();
        $validate = $this->validate($params, ['id' => 'require']);
        if ($validate !== true) {
            $this->error($validate);
        }
        extract($params);
        $res = BankLogic::delAddress($id);
        if ($res) {
            $this->success('成功');
        }
        $this->error('失败');
    }
}
